//
//  GuideDetailViewController.m
//  ITASTRA
//
//  Created by me on 8/12/12.
//  Copyright (c) 2012 me. All rights reserved.
//

#import "GuideDetailViewController.h"
#import "AppDelegate.h"
#import "List.h"

@implementation GuideDetailViewController

@synthesize imageGuideDisplay;
@synthesize descriptionGuideDisplay;
@synthesize titleGuideDisplay;
@synthesize count_straordinariGuideDisplay;
@synthesize identifierGuideDisplay;

@synthesize theList;

AppDelegate *app;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    //
    
    MBProgressHUD *hud = [MBProgressHUD  showHUDAddedTo:self.navigationController.view
                                               animated:YES];
    hud.labelText = @"Fetching";
    
    modal = [[ModalController  alloc] init];
    modal.delegate = self;
    [modal sendTheRequestWithPostString:nil withURLString:[NSString stringWithFormat:@"http://www.italiastraordinaria.it/api/guide/%d.xml",theList.objID]];

    
//    
//    NSLog(@"%@,%@,%d,%@,%@,%@,%@",theList.title,theList.name,theList.objID,theList.info1,theList.info2,theList.has_products,theList.first_image_big);
//    [super viewDidLoad];
//    self.title = theList.name;
//    [[UIBarButtonItem appearance] setTintColor:[UIColor orangeColor]];
//
//    app = [[UIApplication sharedApplication] delegate];
//    
//    //  To catch img address without first "/n"
//    NSString *old_Stra_Photo = theList.image;
//    NSString *path = [old_Stra_Photo stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
//    NSURL *url = [NSURL URLWithString:path];
//    NSData *data = [NSData dataWithContentsOfURL:url];
//    UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 160.0f)];
//    [subview setImage:[UIImage imageWithData:data]];
//    
//    [imageGuideDisplay addSubview:subview];
  //  self.imageGuideDisplay.text = theList.image;
    self.descriptionGuideDisplay.text = theList.description;
    self.count_straordinariGuideDisplay.text = theList.count_straordinari;
    self.titleGuideDisplay.text = theList.title;

    
}

-(void)getdata
{
    [MBProgressHUD  hideHUDForView:self.navigationController.view
                          animated:YES];
    
    
    NSLog(@"~~~~~~%@",[XMLReader  dictionaryForXMLString:modal.stringRx
                                                   error:nil]);
    NSDictionary *dict = [XMLReader  dictionaryForXMLString:modal.stringRx
                                                      error:nil];
    arrayDetail = [[NSMutableArray alloc] init];
    
    if([dict objectForKey:@"objects"])
    {
        if([[dict objectForKey:@"objects"] objectForKey:@"object"])
        {
            if([[[dict objectForKey:@"objects"] objectForKey:@"object"] isKindOfClass:[NSArray  class]])
            {
                arrayDetail =[[NSMutableArray  alloc] initWithArray:[[dict objectForKey:@"objects"] objectForKey:@"object"]];
            }
            else
                [arrayDetail  addObject:[[dict objectForKey:@"objects"] objectForKey:@"object"]];
        }
    }
    
    NSLog(@"%@",arrayDetail);
    [tableView  reloadData];
}

-(void)getError
{
    [MBProgressHUD  hideHUDForView:self.navigationController.view
                          animated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [arrayDetail count];
}

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *CellIdentifier = @"Cell";
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    cell.textLabel.text = [[arrayDetail  objectAtIndex:indexPath.row] objectForKey:@"name"];
    cell.accessoryType = 1;
//    
//	
//	switch(indexPath.section)
//	{
//		case 0:
//            cell.textLabel.text = theList.id;
//			break;
//		case 1:
//			cell.textLabel.text = theList.title;
//			break;
//		case 2:
//			cell.textLabel.text = theList.description;
//			break;
//        case 3:
//			cell.textLabel.text = theList.image;
//			break;
//        case 4:
//            cell.textLabel.text = theList.count_straordinari;
//			break;
//	}
//	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailGuideViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"detailGuid"];
    detailView.mainid = [NSString stringWithFormat:@"%d",theList.objID];
    detailView.catId  = [[arrayDetail  objectAtIndex:indexPath.row] objectForKey:@"id"];

//    theList = [app.listArray objectAtIndex:indexPath.row];
    
//    detailView.theList = theList;
    [self.navigationController pushViewController:detailView animated:YES];
    
    
}
//detailGuid

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//	
//	NSString *sectionName = nil;
//	
//	switch(section)
//	{
//		case 0:
//			sectionName = @"id";
//			break;
//		case 1:
//			sectionName = @"title";
//			break;
//		case 2:
//			sectionName = @"description";
//			break;
//        case 3:
//			sectionName = @"image";
//			break;
//        case 4:
//			sectionName = @"count_straordinari";
//			break;
//
//
//    }
//	
//	return sectionName;
//}
- (void)viewDidUnload {
    [self setImageGuideDisplay:nil];
    [self setDescriptionGuideDisplay:nil];
    [self setTitleGuideDisplay:nil];
    [self setCount_straordinariGuideDisplay:nil];
       
    [super viewDidUnload];
    
}
@end
